package guinong.com.gnw.mvp;

import android.app.Activity;

import java.util.HashMap;

import guinong.com.gnw.base.BasePresenter;
import guinong.com.gnw.entity.LoginEntity;
import guinong.com.gnw.utils.CommonUtils;
import guinong.com.gnw.utils.Constant;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author chenshuangniu
 * @time 2016/11/11 15:00
 * @content
 * @amend
 * @viersion 1.0
 */

public class Login_Presenter extends BasePresenter<Login_View> {
    public Login_Presenter(Activity context, Login_View view) {
        super(context, view);
    }

    public void pushow() {
        toastShortBasic("这是一个测试我的键盘好不好用的测试 不用管  新买的200块 感觉还可以的 鼠标也可以 400块的 用起来感觉还有一些功能还没有摸清楚");
    }

    public void putShowRedToast(String msg) {
        mView.showRedToast(msg);
    }

    //post
    public void loginPost(final String name, final String pwd) {
        if (checkNetwork()) {
            return;
        }
        mPostParams = new HashMap<>(0);
        mPostParams.put("phone", name);
        mPostParams.put("password", CommonUtils.md5(pwd));
        mPostParams.put("uuid", "111111");
        mView.showLoading();
        mGN.getLoginPost(mPostParams)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginEntity>() {
                    @Override
                    public void onCompleted() {
                        mView.stopLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.stopLoading();
                    }

                    @Override
                    public void onNext(LoginEntity entity) {
                        String s = mGson.toJson(entity);
                        if (entity != null) {
                            if (entity.getCode().equals(Constant.REQUEST_STATUS_SUCCESSS)) {
                                mView.toastLongBasic("登录成功\n" + s);
                                mView.enterMain();
                            } else {
                                mView.toastShortBasic(entity.getMessage());
                            }
                        }
                    }
                });
    }

}
